// 显示无人机轨迹
import * as turf from "@turf/turf";
import {GET_MAPBOX_MAP} from "./CreateMap";

const ADD_UAV_LAYER_MAP = (values, bboxInfo) => {
    //测试
    // values = {
    //     "type": "Feature",
    //     "geometry": {
    //         "type": "Polygon",
    //         "coordinates": [[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]]]
    //     }
    // };

    const map = GET_MAPBOX_MAP();
    // 箭头-右
    let svgXML = `<svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg">
<path d="M529.6128 512L239.9232 222.4128 384.7168 77.5168 819.2 512 384.7168 946.4832 239.9232 801.5872z" p-id="9085" fill="#ffffff"></path>
</svg>
`;

    //给图片对象写入base64编码的svg流
    var svgBase64 = 'data:image/svg+xml;base64,' + window.btoa(unescape(encodeURIComponent(svgXML)));

    let arrowIcon = new Image(20, 20);

    arrowIcon.src = svgBase64;

    arrowIcon.onload = function () {
        map.addImage('arrowIcon', arrowIcon);
        SET_ROUTE_DATA();
    };

    let steps = 0;
    let aLength = 0;
    let newRouteGeoJson;
    let routeGeoJson = values;

    // 获取轨迹边界
    var bbox = turf.bbox(routeGeoJson);
    //  测试
    bboxInfo = turf.bboxPolygon(bbox);
    console.log("boooooox----" + bbox); // 输出: [-180.0, -90.0, 180.0, 90.0]
    map.fitBounds(
        [
            [Number(bbox[0]), Number(bbox[1])],
            [Number(bbox[2]), Number(bbox[3])],
        ],
        {
            padding: {top: 100, bottom: 100, left: 100, right: 100},
            maxZoom: 16,
        },
    );

    // 获取轨迹数据
    function SET_ROUTE_DATA() {
        animatePointGeoJson.features[0].geometry.coordinates = routeGeoJson.features[0].geometry.coordinates[0];
        aLength = routeGeoJson.features[0].geometry.coordinates.length;
        newRouteGeoJson = REST_ROUTE(routeGeoJson.features[0], 1000, 'kilometers');
        steps = newRouteGeoJson.geometry.coordinates.length;

        ADD_ROUTE_BBOX_LAYER(); // 添加轨迹边界图层
        ADD_ROUTE_LAYER(); // 添加轨迹线图层
        // addRealRouteSource(); // 添加实时轨迹线图层
        ADD_ARROW_LAYER(); // 添加箭头图层
        // addAnimatePointSource(); // 添加动态点图层
    }

    // 添加轨迹边界图层
    function ADD_ROUTE_BBOX_LAYER() {
        map.addLayer({
            id: 'routeBboxLayer',
            type: 'line',
            source: {
                type: 'geojson',
                data: bboxInfo,
            },
            paint: {
                'line-color': '#fbc219',
                'line-width': 3,
                'line-dasharray': [5, 5],
                'line-offset': -10,
            },
        });
    }

    // 添加轨迹线图层
    function ADD_ROUTE_LAYER() {
        map.addLayer({
            id: 'routeLayer',
            type: 'line',
            source: {
                type: 'geojson',
                lineMetrics: true,
                data: routeGeoJson,
            },
            paint: {
                'line-width': 10,
                'line-opacity': 1,
                'line-color': '#009EFF',
            },
        });
    }

    // 添加箭头图层
    function ADD_ARROW_LAYER() {
        map.addLayer({
            id: 'arrowLayer',
            type: 'symbol',
            source: {
                type: 'geojson',
                data: routeGeoJson, //轨迹geojson格式数据
            },
            layout: {
                'symbol-placement': 'line',
                'symbol-spacing': 50, // 图标间隔，默认为250
                'icon-image': 'arrowIcon', //箭头图标
                'icon-size': 0.5,
            },
        });
    }

    function REST_ROUTE(route, nstep, units) {
        var newroute = {
            type: 'Feature',
            geometry: {
                type: 'LineString',
                coordinates: [],
            },
        };

        var lineDistance = turf.length(route);
        var nDistance = lineDistance / nstep;

        for (let i = 0; i < aLength - 1; i++) {
            var from = turf.point(route.geometry.coordinates[i]);
            var to = turf.point(route.geometry.coordinates[i + 1]);
            let lDistance = turf.distance(from, to, {
                units: units,
            });

            if (i == 0) {
                newroute.geometry.coordinates.push(route.geometry.coordinates[0]);
            }

            if (lDistance > nDistance) {
                let rings = LINE_MORE(from, to, lDistance, nDistance, units);

                newroute.geometry.coordinates = newroute.geometry.coordinates.concat(rings);
            } else {
                newroute.geometry.coordinates.push(route.geometry.coordinates[i + 1]);
            }
        }

        return newroute;
    }

    function LINE_MORE(from, to, distance, splitLength, units) {
        var step = parseInt(distance / splitLength);
        var leftLength = distance - step * splitLength;
        var rings = [];
        var route = turf.lineString([from.geometry.coordinates, to.geometry.coordinates]);

        for (let i = 1; i <= step; i++) {
            let nlength = i * splitLength;
            let pnt = turf.along(route, nlength, {
                units: units,
            });

            rings.push(pnt.geometry.coordinates);
        }

        if (leftLength > 0) {
            rings.push(to.geometry.coordinates);
        }

        return rings;
    }
};
